<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=799315
-->
<head>
  <title>Test for MP4 and MP3 support</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>

<video id="v"></video>

<pre id="test">
<script>

function check_mp4(v, enabled) {
  function check(type, expected) {
    var ex = enabled ? expected : "";
    is(v.canPlayType(type), ex, type + "='" + ex + "'");
  }

  check("video/mp4", "maybe");
  check("video/x-m4v", "maybe");
  check("audio/mp4", "maybe");
  check("audio/x-m4a", "maybe");

  // Not the MIME type that other browsers respond to, so we won't either.
  check("audio/m4a", "");
  check("video/m4v", "");

  check("audio/aac", "maybe");
  check("audio/aacp", "maybe");

  // H.264 Constrained Baseline Profile Level 3.0, AAC-LC
  check("video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"", "probably");

  // H.264 Constrained Baseline Profile Level 3.0, mp3
  check("video/mp4; codecs=\"avc1.42E01E, mp3\"", "probably");

  check("video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"", "probably");
  check("video/mp4; codecs=\"avc1.58A01E, mp4a.40.2\"", "probably");

  // H.264 Main Profile Level 3.0, AAC-LC
  check("video/mp4; codecs=\"avc1.4D401E, mp4a.40.2\"", "probably");
  // H.264 Main Profile Level 3.1, AAC-LC
  check("video/mp4; codecs=\"avc1.4D401F, mp4a.40.2\"", "probably");
  // H.264 Main Profile Level 4.0, AAC-LC
  check("video/mp4; codecs=\"avc1.4D4028, mp4a.40.2\"", "probably");
  // H.264 High Profile Level 3.0, AAC-LC
  check("video/mp4; codecs=\"avc1.64001E, mp4a.40.2\"", "probably");
  // H.264 High Profile Level 3.1, AAC-LC
  check("video/mp4; codecs=\"avc1.64001F, mp4a.40.2\"", "probably");

  check("video/mp4; codecs=\"avc1.42E01E\"", "probably");
  check("video/mp4; codecs=\"avc1.42001E\"", "probably");
  check("video/mp4; codecs=\"avc1.58A01E\"", "probably");
  check("video/mp4; codecs=\"avc1.4D401E\"", "probably");
  check("video/mp4; codecs=\"avc1.64001F\"", "probably");

  // AAC-LC
  check("audio/mp4; codecs=\"mp4a.40.2\"", "probably");
  check("audio/mp4; codecs=mp4a.40.2", "probably");
  check("audio/x-m4a; codecs=\"mp4a.40.2\"", "probably");
  check("audio/x-m4a; codecs=mp4a.40.2", "probably");

  check("audio/mp4; codecs=\"mp4a.40.2,\"", ""); // Invalid codecs string

  // HE-AAC v1
  check("audio/mp4; codecs=\"mp4a.40.5\"", "probably");
  check("audio/mp4; codecs=mp4a.40.5", "probably");
  check("audio/x-m4a; codecs=\"mp4a.40.5\"", "probably");
  check("audio/x-m4a; codecs=mp4a.40.5", "probably");
  // HE-AAC v2
  check("audio/mp4; codecs=\"mp4a.40.29\"", "probably");

  // Opus
  check("audio/mp4; codecs=\"opus\"", "probably");
  check("audio/mp4; codecs=opus", "probably");

  // Flac.
  // Not available on Android yet.
  var expectedResult = IsSupportedAndroid() ? "" : "probably";
  check("audio/mp4; codecs=\"flac\"", expectedResult);
  check("audio/mp4; codecs=flac", expectedResult);
}

function check_mp3(v, enabled) {
  function check(type, expected) {
    var ex = enabled ? expected : "";
    is(v.canPlayType(type), ex, type + "='" + ex + "'");
  }

  check("audio/mpeg", "maybe");
  check("audio/mp3", "maybe");

  check("audio/mpeg; codecs=\"mp3\"", "probably");
  check("audio/mpeg; codecs=mp3", "probably");

  check("audio/mp3; codecs=\"mp3\"", "probably");
  check("audio/mp3; codecs=mp3", "probably");
}

function IsWindowsVistaOrLater() {
  var re = /Windows NT (\d+\.\d)/;
  var winver = navigator.userAgent.match(re);
  return winver && winver.length == 2 && parseFloat(winver[1]) >= 6.0;
}

function IsMacOSSnowLeopardOrLater() {
  var re = /Mac OS X (\d+)\.(\d+)/;
  var ver = navigator.userAgent.match(re);
  if (!ver || ver.length != 3) {
    return false;
  }
  var major = ver[1] | 0;
  var minor = ver[2] | 0;
  return major == 10 && minor >= 6;
}

function IsLinux() {
  return navigator.userAgent.indexOf("Linux") != -1;
}

function getPref(name) {
  var pref = false;
  try {
    pref = SpecialPowers.getBoolPref(name);
  } catch(ex) { }
  return pref;
}

function IsSupportedAndroid() {
  return getAndroidVersion() >= 14;
}

function IsJellyBeanOrLater() {
  return getAndroidVersion() >= 16;
}

var haveMp4 = (getPref("media.wmf.enabled") && IsWindowsVistaOrLater()) ||
              IsMacOSSnowLeopardOrLater() ||
              (IsSupportedAndroid() &&
               (IsJellyBeanOrLater() || getPref("media.plugins.enabled"))) ||
              (IsLinux() && getPref("media.ffmpeg.enabled"));

check_mp4(document.getElementById('v'), haveMp4);

var haveMp3 = getPref("media.wmf.enabled") ||
              (IsLinux() && getPref("media.ffmpeg.enabled")) ||
              (IsSupportedAndroid() &&
               ((IsJellyBeanOrLater() && getPref("media.android-media-codec.enabled")) ||
                getPref("media.plugins.enabled"))) ||
              getPref("media.apple.mp3.enabled");
check_mp3(document.getElementById('v'), haveMp3);

mediaTestCleanup();
</script>
</pre>
</body>
</html>
